package com.trendplus.unitconverter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class WeightCalculate extends Activity {
	Spinner lengthType=null; 
	TextView mTextView;
	private EditText inputValue;
	private Button button_calc;
	private Button button_cleanResult;
	/** Called when the activity is first created. */
	//catty:台斤, Taiwan Tael:台兩, 
    static final String[] weightTypeList=new String[] { 
        "公克", "公斤", "公噸", "台兩","台斤","盎司","磅"
    };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.calc_weight);
	    findViews();
	    touchSpinnerS();
	    setListeners();
	}
	public void findViews(){
		inputValue = (EditText) findViewById(R.id.inputValue);
		button_calc = (Button) findViewById(R.id.submit);
		button_cleanResult = (Button) findViewById(R.id.cleanData);
	}
	//Listen for button clicks
    private void setListeners() {
        button_calc.setOnClickListener(calcWeight);
        button_cleanResult.setOnClickListener(cleanEditText);
    }
	
    public void touchSpinnerS(){
		Spinner SpinnerS = (Spinner)findViewById(R.id.mySpinner);

        //設定功能表項目陣列，使用createFromResource()
        //ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.items, android.R.layout.simple_spinner_item);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, weightTypeList);

        //設定選單
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mTextView = (TextView) findViewById(R.id.textViewWeight);
        //設定adapter
        SpinnerS.setAdapter(adapter);
        
        SpinnerS.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                mTextView.setText(weightTypeList[arg2]);
                arg0.setVisibility(View.VISIBLE);
            }
 
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
 
        });
	}
    
    private OnClickListener calcWeight = new OnClickListener(){
		public void onClick(View v) {
			TextView weightType = (TextView)findViewById(R.id.textViewWeight);
			EditText inputValue = (EditText)findViewById(R.id.inputValue);
			String weightTypes = weightType.getText().toString();
			long inputValues = Long.parseLong((inputValue.getText().toString()));
			System.out.println("weightTypes="+weightTypes+", values="+inputValues);
			
			converterOfWeight(weightTypes,inputValues);
		}
    };
    
    public void converterOfWeight(String weightTypes, double value){
    	
    	
    	String gResultStr = "";
    	String kgResultStr = "";
    	String mtResultStr = "";
    	String ttResultStr = "";
    	String tgResultStr = "";
    	String ozResultStr = "";
    	String poundResultStr = "";

    	if(weightTypes.equals("公克")){
    		gResultStr = Double.toString(value);
    		kgResultStr = Double.toString(value*0.001);
    		mtResultStr = Double.toString(value*0.000001);
    		ttResultStr = Double.toString(value*0.02667);
    		tgResultStr = Double.toString(value*0.00167);
    		ozResultStr = Double.toString(value*0.03527);
    		poundResultStr = Double.toString(value*0.00221);
    		resultSetting(gResultStr, kgResultStr, mtResultStr, ttResultStr, tgResultStr, ozResultStr, poundResultStr);
    		
    		
    	}else if(weightTypes.equals("公斤")){
    		gResultStr = Double.toString(value*1000);
    		kgResultStr = Double.toString(value);
    		mtResultStr = Double.toString(value*0.001);
    		ttResultStr = Double.toString(value*26.6667);
    		tgResultStr = Double.toString(value*1.66667);
    		ozResultStr = Double.toString(value*35.274);
    		poundResultStr = Double.toString(value*2.20462);
    		
    		resultSetting(gResultStr, kgResultStr, mtResultStr, ttResultStr, tgResultStr, ozResultStr, poundResultStr);
    		
    	}else if(weightTypes.equals("公噸")){
    		gResultStr = Double.toString(value*1000*1000);
    		kgResultStr = Double.toString(value*1000);
    		mtResultStr = Double.toString(value);
    		ttResultStr = Double.toString(value*26666.7);
    		tgResultStr = Double.toString(value*1666.67);
    		ozResultStr = Double.toString(value*35274);
    		poundResultStr = Double.toString(value*2204.62);
    		
    		resultSetting(gResultStr, kgResultStr, mtResultStr, ttResultStr, tgResultStr, ozResultStr, poundResultStr);
    		
    	}else if(weightTypes.equals("台斤")){
    		gResultStr = Double.toString(value*600);
    		kgResultStr = Double.toString(value*0.6);
    		mtResultStr = Double.toString(value*0.0006);
    		ttResultStr = Double.toString(value*16);
    		tgResultStr = Double.toString(value);
    		ozResultStr = Double.toString(value*21.1644);
    		poundResultStr = Double.toString(value*1.32277);
    		
    		resultSetting(gResultStr, kgResultStr, mtResultStr, ttResultStr, tgResultStr, ozResultStr, poundResultStr);
    		
    	}else if(weightTypes.equals("台兩")){
    		gResultStr = Double.toString(value*37.5);
    		kgResultStr = Double.toString(value*0.0375);
    		mtResultStr = Double.toString(value*0.00004);
    		ttResultStr = Double.toString(value);
    		tgResultStr = Double.toString(value*0.0625);
    		ozResultStr = Double.toString(value*1.32277);
    		poundResultStr = Double.toString(value*0.08267);
    		
    		resultSetting(gResultStr, kgResultStr, mtResultStr, ttResultStr, tgResultStr, ozResultStr, poundResultStr);
    		
    	}else if(weightTypes.equals("盎司")){
    		gResultStr = Double.toString(value*28.3495);
    		kgResultStr = Double.toString(value*0.02835);
    		mtResultStr = Double.toString(value*0.00003);
    		ttResultStr = Double.toString(value*0.75599);
    		tgResultStr = Double.toString(value*0.04725);
    		ozResultStr = Double.toString(value);
    		poundResultStr = Double.toString(value*0.0625);
    		
    		resultSetting(gResultStr, kgResultStr, mtResultStr, ttResultStr, tgResultStr, ozResultStr, poundResultStr);
    		
    	}else if(weightTypes.equals("磅")){
    		gResultStr = Double.toString(value*453.592);
    		kgResultStr = Double.toString(value*0.45359);
    		mtResultStr = Double.toString(value*0.00045);
    		ttResultStr = Double.toString(value*12.0958);
    		tgResultStr = Double.toString(value*0.75599);
    		ozResultStr = Double.toString(value*16);
    		poundResultStr = Double.toString(value);
    		
    		resultSetting(gResultStr, kgResultStr, mtResultStr, ttResultStr, tgResultStr, ozResultStr, poundResultStr);
    	}

    	
    }
    
    private OnClickListener cleanEditText = new OnClickListener(){

		public void onClick(View v) {
			resultSetting("__", "__", "__", "__", "__", "__", "__");
		}
    };
    
  //結果填入欄位內
	public void resultSetting(String gResultStr, String kgResultStr, String mtResultStr, 
			String ttResultStr, String tgResultStr, String ozResultStr, String poundResultStr){
		
		TextView gResult = (TextView)findViewById(R.id.gResult);
    	TextView kgResult = (TextView)findViewById(R.id.kgResult);
    	TextView mtResult = (TextView)findViewById(R.id.mtResult);
    	TextView ttResult = (TextView)findViewById(R.id.ttResult);
    	TextView tgResult = (TextView)findViewById(R.id.tgResult);
    	TextView ozResult = (TextView)findViewById(R.id.ozResult);
    	TextView poundResult = (TextView)findViewById(R.id.poundResult);
		
		gResult.setText("="+ gResultStr +"公克");
		kgResult.setText("="+ kgResultStr +"公斤");
		mtResult.setText("="+ mtResultStr +"公噸");
		ttResult.setText("="+ ttResultStr +"台兩");
		tgResult.setText("="+ tgResultStr +"台斤");
		ozResult.setText("="+ ozResultStr +"盎司");
		poundResult.setText("="+ poundResultStr +"磅");
	}
	
}
